const DEF_TRACK_INDEX = 0;
var YYSpine = function () {

}
var pro = YYSpine.prototype;
/**
 * 播放龙骨动画,不需要循环回调的
 * @param node 龙骨节点
 * @param name 动画名
 * @param playTime 播放次数
 * @param isRefech 骨骼和皮肤是否归位
 * 大部分情况下都是需要归位的，但是有一些特殊的情况为了保持连贯性是不做重置的
 * @param complete
 */
pro.playAnimation = function(node,name,playTime,isRefech,complete){
    let skeleton = node.getComponent(sp.Skeleton);
    if(isRefech){
        skeleton.setBonesToSetupPose();
        skeleton.setSlotsToSetupPose();
    }
    skeleton.clearTracks();
    let curName = name;
    if (playTime <= 0){
        skeleton.setAnimation(DEF_TRACK_INDEX,name,true);
    }else{
        if(playTime === 1){
            skeleton.setCompleteListener(function (event) {
                if (curName === event.animation.name){
                    skeleton.setCompleteListener(null);
                    if (complete){
                        complete();
                    }
                }
            });
            skeleton.setAnimation(DEF_TRACK_INDEX,name,false);
        }else{
            let count = playTime;
            let curCount = 0;
            skeleton.setCompleteListener(function () {
                curCount++;
                if (curName === event.animation.name
                    && curCount === count && complete){//动画结束
                    skeleton.setEndListener(null);
                    complete();
                }
            });
        }
    }
}
/**
 * 播放循环动画
 * @param node
 * @param name
 */
pro.playLoopAnimation = function(node,name){
    let skeleton = node.getComponent(sp.Skeleton);
    // skeleton.clearTracks();
    cc.log('播放动画:'+name);
    skeleton.setAnimation(DEF_TRACK_INDEX,name,true);
    skeleton.setEndListener(null);
    skeleton.setCompleteListener(null);
}


pro.setSpinePlayTime = function(node,time){
    let skeleton = node.getComponent(sp.Skeleton);
    skeleton.timeScale = time;
}

pro.setSpineSkin = function(node,name){
    let skeleton = node.getComponent(sp.Skeleton);
    skeleton.setSkin(name);
}

// yySpine

pro.getSpinePlayName = function(node){
    let skeleton = node.getComponent(sp.Skeleton);
    return skeleton.getState().tracks[0].animation.name;
}

// pro.getSpineSkin = function(node){
//     let skeleton = node.getComponent(sp.Skeleton);
//     console.log()
//     // return skeleton.getAttachment();
// }






module.exports = new YYSpine();